package com.fearless.crm.workbench.service.impl;

import com.fearless.crm.utils.UUIDUtil;
import com.fearless.crm.vo.ContactPaginationVo;
import com.fearless.crm.workbench.dao.ContactsDao;
import com.fearless.crm.workbench.dao.CustomerDao;
import com.fearless.crm.workbench.entity.Contacts;
import com.fearless.crm.workbench.entity.Customer;
import com.fearless.crm.workbench.service.ContactsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ContactsServiceImpl implements ContactsService {


    @Autowired
    private ContactsDao contactsDao;

    @Autowired
    private CustomerDao customerDao;



//根据联系人名称，模糊查询联系人 业务层方法
    public List<Contacts> searchContactsByNameService(String cname) {

        List<Contacts> contactsList=contactsDao.searchContactsByNameDao(cname);

        return contactsList;
    }

//展现联系人列表,业务层方法
    public ContactPaginationVo getContactListService(Map map) {
        int total=contactsDao.getContactTotal();
        List<Contacts> contactsList=contactsDao.getContactList(map);

        ContactPaginationVo contactPaginationVo=new ContactPaginationVo();
        contactPaginationVo.setTotal(total);
        contactPaginationVo.setDataList(contactsList);

        return contactPaginationVo;
    }

//在客户的详细页展现联系人列表
    public List<Contacts> getContactListByCustomerId(String id) {

        List<Contacts> contacts=contactsDao.getContactListByCustomerId(id);
        return contacts;
    }

//执行在客户详细页删除联系人的操作
    public boolean deleteContactInCustomerDetail(String contactId) {
        boolean flag=true;
        int count=contactsDao.deleteContactInCustomerDetail(contactId);
        if(count!=1){
            flag=false;
        }
        return flag;
    }

//在联系人首页执行新增联系人的操作
    public boolean saveContact(Map map) {
        boolean flag=true;


        //先判断填写的客户名称是否存在
        String customerName= (String) map.get("customerName");
        Contacts contact= (Contacts) map.get("contact");


        Customer customer=customerDao.queryCustomer(customerName);
        if(customer==null){
            new Customer();
            customer=new Customer();
            customer.setId(UUIDUtil.getUUID());
            customer.setName(customerName);
            customer.setCreateBy(contact.getCreateBy());
            customer.setCreateTime(contact.getCreateTime());
            customer.setOwner(contact.getOwner());

            int count=customerDao.saveCustomerDao(customer);
            if(count!=1){
                flag=false;
            }
        }

        contact.setCustomerId(customer.getId());
        int count2=contactsDao.saveNewContact(contact);
        if(count2!=1){
            flag=false;
        }



        return flag;
    }

//铺垫修改联系人的模态窗口
    public Map<String,Object> editContact(String id) {
       Contacts contacts=contactsDao.queryContactById(id);
       Customer customer=customerDao.queryCustomerById(contacts.getCustomerId());



        Map map=new HashMap();
        map.put("contacts",contacts);
        if(customer!=null){
            map.put("customer",customer);
        }else{
            map.put("customer","");
        }


        return map;
    }

//执行修改联系人的操作
    public boolean updateContact(Contacts contacts) {
        boolean flag=true;

            String customerId=customerDao.queryOneCustomer(contacts.getCustomerId());

            contacts.setCustomerId(customerId);
            int count=contactsDao.updateContactDao(contacts);

            if(count!=1){
                flag=false;
            }

        return flag;
    }

//执行批量删除联系人的操作
    public boolean deleteContact(String[] ids) {
        boolean flag=true;
        int count=contactsDao.deleteBatchContact(ids);
        if (count == 0) {
            flag=false;
        }
        return flag;
    }

//联系人的详细页铺垫
    public Contacts contactDetail(String id) {
        Contacts contacts = contactsDao.queryContactById(id);


        return contacts;
    }
}
